package com.iwhalecloud.bss.kite.cucc.service.custview.evolve.task;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteDateUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.CustViewBaseVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.CustViewVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.RentInfo;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.RentInfoVO;
import com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler.SuddenCycleLinkHandler;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import com.ztesoft.zsmart.core.log.ZSmartLogger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description 起止租信息
 * @Author deng.gang
 */
public class RentInfoTask extends AbstractCustViewTask{
    private static final ZSmartLogger LOGGER = ZSmartLogger.getLogger(RentInfoTask.class);

    @Override
    public CustViewBaseVO call(Object[] args) {
        if (args.length != 2) {
            return null;
        }
        Map<String, Object> userDetail = (Map<String, Object>) args[0];
        CustViewVO custViewVO = (CustViewVO) args[1];

        RentInfoVO rentInfoVO = new RentInfoVO();
        custViewVO.setRent(rentInfoVO);

        List<RentInfo> rentInfoList = new ArrayList<>();
        if (KiteMapUtils.isNotEmpty(userDetail)) {
            List<Map<String, Object>> userSvcStateInfos = (List<Map<String, Object>>) userDetail.get("USER_SVCSTATE_INFOS");
            if (KiteListUtils.isNotEmpty(userSvcStateInfos)) {
                userSvcStateInfos.forEach(userSvcState -> {
                    RentInfo rentInfo = new RentInfo();
                    String stateCode = KiteMapUtils.getString(userSvcState, "SERVICE_STATE_CODE");
                    DcPublic dcPublic = DcPublicCache.getByPcode("20200201001", stateCode).stream().findFirst().orElse(null);
                    if (Objects.nonNull(dcPublic)) {
                        stateCode = dcPublic.getCodeb();
                    }
                    rentInfo.setUserState(stateCode);
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                    String startDate = KiteMapUtils.getString(userSvcState, "START_DATE");
                    String endDate = KiteMapUtils.getString(userSvcState, "END_DATE");
                    String updateDate = KiteMapUtils.getString(userSvcState, "UPDATE_TIME");
                    Date start = null;
                    Date end = null;
                    Date upd = null;
                    try {
                        start = KiteStringUtils.isNotEmpty(startDate) ? simpleDateFormat.parse(startDate) : null;
                        end = KiteStringUtils.isNotEmpty(endDate) ? simpleDateFormat.parse(endDate) : null;
                        upd = KiteStringUtils.isNotEmpty(updateDate) ? simpleDateFormat.parse(updateDate) : null;
                    } catch (ParseException e) {
                        LOGGER.error("格式化时间失败-->", e, e.getMessage());
                        ErrorConsts.BUSI_COMMON_ERROR.throwOut("格式化时间失败");
                    }
                    rentInfo.setStartDate(Objects.nonNull(start) ? KiteDateUtils.formatDate(start, KiteDateUtils.DATE_TIME_FORMAT) : null);
                    rentInfo.setEndDate(Objects.nonNull(end) ? KiteDateUtils.formatDate(end, KiteDateUtils.DATE_TIME_FORMAT) : null);
                    rentInfo.setUpdateDate(Objects.nonNull(upd) ? KiteDateUtils.formatDate(upd, KiteDateUtils.DATE_TIME_FORMAT) : null);
                    rentInfoList.add(rentInfo);
                });
                rentInfoVO.setRents(rentInfoList);
            }
        }
        return rentInfoVO;
    }
}
